<script>
	export default {
		onLaunch: function() {
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		
		post: function(url, data, method, codetype) {
			let t = this;
			let codeTypes = 'application/x-www-form-urlencoded;charset=UTF-8';
			if (codetype == 1) {
				// 发送验证码
				codeTypes = 'application/x-www-form-urlencoded;charset=UTF-8'
			} else if (codetype == 2) {
				codeTypes = 'application/json'
			} else if (codetype == 3) {
				codeTypes = 'application/form-data'
			} else if (codetype == 4) {
				codeTypes = 'multipart/form-data'
			} else {
				codeTypes = 'application/x-www-form-urlencoded;charset=UTF-8'
			}
			var promise = new Promise((resolve, reject) => {
				//init
				let header = {
					// 'content-type': 'application/form-data',
					'content-type': codeTypes,
					'token': uni.getStorageSync('token') || ''
				};
				// console.log(header)
				// console.log(data)
				// let method = '';
				// console.log(url + url)
				// console.log(token)
				// uni.showLoading({
				//         title: '加载中',
				// })
				//网络请求
				// console.log(data)
				uni.getNetworkType({
					success(r) {
						if (r.networkType == 'none') {
							uni.showToast({
								title: '您还没有连接网络哦，请先连接网络吧~',
								icon: 'none',
								duration: 1300
							})
						} else {
							// console.log('66666666666')
							const baseUrl = 'http://tanggu-hospital.brofirst.cn/'
							uni.request({
								url: baseUrl + url,
								data: data,
								method: method || 'POST',
								header: header,
								emulateJSON: true,
								success: function(res) {
									//返回取得的数据
									// console.log(':::::::::::::::::::', url);
									// console.log(res);
									// uni.hideLoading();
									if (res.data.code == 0) {
										resolve(res.data);
									} else {
										resolve(res.data);
									}
								},
								fail: function(e) {
									console.log(e);
									// console.log('88888888888888888')
									// uni.hideLoading();
									reject('网络出错');
									uni.hideNavigationBarLoading();
									uni.showToast({
										title: '网络要挤爆啦，请稍后重试吧~',
										icon: 'none',
										duration: 1300
									})
								},
								complete: function(daa) {
		
								}
							});
						}
					}
				});
		
			});
			return promise;
		},
		
		
		//上传图片
		upload(filetype, file) {
			let that = this
			var promise = new Promise((resolve, reject) => {
				// wx.showNavigationBarLoading()
				wx.showLoading({
					title: '上传中',
				})
				let url = that.globalData.uploadurl + 'common/upload';
				console.log(url)
				let head = {
					'token': uni.getStorageSync('qizhibangToken'),
					'XX-Device-Type': ''
				}
				console.log(head)
				let typename = {
					filetype: filetype
				}
				uni.uploadFile({
					url: url, //仅为示例，非真实的接口地址
					filePath: file,
					name: 'file',
					header: head,
					formData: typename,
					success: function(res) {
						console.log('上传文件后', res)
						let temdata = JSON.parse(res.data);
						console.log(temdata)
						let urlobj = {
							url: temdata.data.yuan,
							showurl:temdata.data.dai
						}
						resolve(urlobj);
					},
					fail: function(res) {
						reject('网络出错');
						wx.hideNavigationBarLoading()
						wx.hideLoading()
					},
					complete: () => {
						wx.hideNavigationBarLoading()
						wx.hideLoading()
					},
				})
			});
			return promise;
		},
	}
</script>

<style>
	/*每个页面公共css */
</style>
